﻿using System;
using System.Collections;
using System.Threading;

namespace HttpModuleAccessControl
{
    public class Users : CollectionBase, IEnumerable
    {
        static ReaderWriterLock rwlock = new ReaderWriterLock();

        /// <summary>
        /// Default constructor. Do nothing in this version.
        /// </summary>
        public Users()
        { }

        /// <summary>
        /// Load users information from respository.
        /// </summary>
        /// <param name="loadUsersFromDB"></param>
        public Users(bool loadUsersFromDB)
        {
            try
            {
                Users usrs = Database.GetUsers();
                rwlock.AcquireWriterLock(500);
                if (List.Count >= 0)
                    List.Clear();

                foreach (User usr in usrs)
                {
                    List.Add(usr);
                }
                rwlock.ReleaseWriterLock();
            }
            catch (Exception ex)
            {
                Log.Write("Users.cs, contstructor , Exception = " + ex.Message);
            }



        }

        /// <summary>
        /// Add a user into the User list.
        /// </summary>
        /// <param name="user"></param>
        public void Add(User user)
        {
            rwlock.AcquireWriterLock (500);

            List.Add(user);
            rwlock.ReleaseWriterLock ();

        }

        /// <summary>
        /// Remove a user from the User list by index position.
        /// </summary>
        /// <param name="index"></param>
        public void Remove(int index)
        {
            if (index > Count - 1 || index < 0)
            {
                //throw new Exception("Index out of bounds");
                return;
            }
            rwlock.AcquireWriterLock (1000);
            List.RemoveAt(index);
            rwlock.ReleaseWriterLock();
        }

        /// <summary>
        /// Return a user instance by index position in the Users list.
        /// </summary>
        /// <param name="Index"></param>
        /// <returns></returns>
        public User Item(int Index)
        {
            rwlock.AcquireReaderLock (500);
            User u = (User)List[Index];
            rwlock.ReleaseReaderLock();
            return u;
        }

        /// <summary>
        /// Get index position in the User list by his user ID.
        /// </summary>
        /// <param name="UserID"></param>
        /// <returns></returns>
        public int FindUserListIndex(string UserID)
        {
            int _answer = -1;

            rwlock.AcquireReaderLock(500);
            for (int i = 0; i < List.Count; i++)
            {
                User usr = (User)List[i];
                if (usr.UserID.ToUpper().Trim() == UserID.ToUpper().Trim())
                {
                    _answer = i;
                    break;
                }
            }
            rwlock.ReleaseReaderLock();

            return _answer;
        }

        /// <summary>
        /// Check whether a user exists in the User list
        /// </summary>
        /// <param name="NameOrID">UserID or Username</param>
        /// <returns></returns>
        public bool Contains(string NameOrID)
        {
            bool _answer = false;
            rwlock.AcquireReaderLock(500);
            for (int index = 0; index < Count; index++)
            {
                if (((User)List[index]).Name.ToUpper().Trim() == NameOrID.ToUpper().Trim())
                {
                    _answer = true;
                    break;
                }
                if (((User)List[index]).UserID.ToUpper().Trim() == NameOrID.ToUpper().Trim())
                {
                    _answer = true;
                    break;
                }
            }
            rwlock.ReleaseReaderLock();
            return _answer;
        }

        /// <summary>
        /// Authenticate a user by checking username/password.
        /// You may encrypt/decrypt passwords in this function for future modification.
        /// </summary>
        /// <param name="UserID"></param>
        /// <param name="Password"></param>
        /// <returns></returns>
        public bool Validate(string UserID, string Password)
        {
            bool _answer = false;

            rwlock.AcquireReaderLock(500);
            for (int i = 0; i < FirstClass._FirstClassUsers.Count; i++)
            {
                if ((UserID == FirstClass._FirstClassUsers.Item(i).UserID && (Password == FirstClass._FirstClassUsers.Item(i).Password)))
                {
                    _answer = true;
                    break;
                }
            }
            rwlock.ReleaseReaderLock();
            return _answer;
        }


        #region Not use in this version

        //public void Remove(string NameOrID)
        //{
        //    for (int index = 0; index < Count; index++)
        //    {
        //        if (((User)List[index]).Name.ToUpper().Trim() == NameOrID.ToUpper().Trim())
        //        {
        //            Remove(index);
        //            break;
        //        }
        //        if (((User)List[index]).UserID.ToUpper().Trim() == NameOrID.ToUpper().Trim())
        //        {
        //            Remove(index);
        //            break;
        //        }
        //    }

        //}
        #endregion

 
    }
}
